<?php

namespace App\Domain\LifeHealth\Services;

use App\Domain\LifeHealth\Dao\LifeHealthDao;

class LiftHealthService
{
    public function findBySmokeUid($uid)
    {
        $healthrecordDao = new LifeHealthDao();
        $data = $healthrecordDao->findBySmokeUid($uid);
        $res = [
            'hasSmoking' => $data['hasSmoking'],
            'years' => $data['years'],
            'number' => $data['number'],
            'quitYears' => $data['quitYears'],
        ];
        return $res;
    }

    public function findBySportUid($uid)
    {
        $healthrecordDao = new LifeHealthDao();
        $data = $healthrecordDao->findBySportUid($uid);
        $res = [
            'hasSport' => $data['hasSport'],
            'times' => $data['times'],
        ];
        return $res;
    }


    public function findBySleepUid($uid)
    {
        $healthrecordDao = new LifeHealthDao();
        $data = $healthrecordDao->findBySleepUid($uid);
        $res = [
            'sleepingTimes' => $data['sleepingTimes'],
            'startTime' => $data['startTime'],
            'getupTime' => $data['getupTime'],
            'hasSnore' => $data['hasSnore'],
        ];
        return $res;
    }

    public function findByDrinkUid($uid)
    {
        $healthrecordDao = new LifeHealthDao();
        $data = $healthrecordDao->findByDrinkUid($uid);
        $res = [
            'drinkYears' => $data['drinkYears'],
            'quitYears' => $data['quitYears'],
            'hasDrinking' => $data['hasDrinking'],
            'names' => $data['drinking_type'],
            'values' => $data['drinking_daily'],
        ];
        return $res;
    }

    public function findByFlavorUid($uid)
    {
        $healthrecordDao = new LifeHealthDao();
        $data = $healthrecordDao->findByFlavorUid($uid);
        $res = [
            'flavorKind' => $data['flavorKind'],
            'names' => $data['name'],
            'values' => $data['number'],
        ];
//        $name = explode(',', $data['name']);
//        $number = explode(',', $data['number']);
//        $list = array_combine($name, $number);
//        $res = [
//            'flavorKind' => $data['flavorKind'],
//            'list' => $list
//        ];
        return $res;
    }

    public function Smoke($param)
    {
        $healthrecordDao = new LifeHealthDao();
        $data = $healthrecordDao->findBySmokeUid($param['memberUid']);
        if ($data) {
            $res = [
                'uid' => $param['memberUid'],
                'hasSmoking' => $param['hasSmoking'],
                'years' => $param['years'],
                'number' => $param['number'],
                'quitYears' => $param['quitYears']
            ];
            $data = $healthrecordDao->updateSmoke($data, $res);
        } else {
            $data = $healthrecordDao->createSmoke([
                'uid' => $param['memberUid'],
                'hasSmoking' => $param['hasSmoking'],
                'years' => $param['years'],
                'number' => $param['number'],
                'quitYears' => $param['quitYears']
            ]);
        }
        return $data;
    }

    public function Sport($param)
    {
        $healthrecordDao = new LifeHealthDao();
        $data = $healthrecordDao->findBySportUid($param['memberUid']);
        if ($data) {
            $res = [
                'uid' => $param['memberUid'],
                'hasSport' => $param['hasSport'],
                'times' => $param['times'],
            ];
            $data = $healthrecordDao->updateSport($data, $res);
        } else {
            $data = $healthrecordDao->createSport([
                'uid' => $param['memberUid'],
                'hasSport' => $param['hasSport'],
                'times' => $param['times'],
            ]);
        }
        return $data;
    }

    public function Sleep($param)
    {
        $healthrecordDao = new LifeHealthDao();
        $data = $healthrecordDao->findBySleepUid($param['memberUid']);
        if ($data) {
            $res = [
                'uid' => $param['memberUid'],
                'sleepingTimes' => $param['sleepingTimes'],
                'startTime' => $param['startTime'],
                'getupTime' => $param['getupTime'],
                'hasSnore' => $param['hasSnore'],
            ];
            $data = $healthrecordDao->updateSleep($data, $res);
        } else {
            $data = $healthrecordDao->createSleep([
                'uid' => $param['memberUid'],
                'sleepingTimes' => $param['sleepingTimes'],
                'startTime' => $param['startTime'],
                'getupTime' => $param['getupTime'],
                'hasSnore' => $param['hasSnore'],
            ]);
        }
        return $data;
    }

    public function Drink($param)
    {
        $healthrecordDao = new LifeHealthDao();
        $data = $healthrecordDao->findByDrinkUid($param['memberUid']);
        if ($data) {
            $res = [
                'uid' => $param['memberUid'],
                'hasDrinking' => $param['hasDrinking'],
                'drinkYears' => $param['years'],
                'quitYears' => $param['quitYears'],
                'drinking_type' => $param['names'],
                'drinking_daily' => $param['values'],
            ];
            $data = $healthrecordDao->updateDrink($data, $res);
        } else {
            $data = $healthrecordDao->createDrink([
                'uid' => $param['memberUid'],
                'hasDrinking' => $param['hasDrinking'],
                'drinkYears' => $param['years'],
                'quitYears' => $param['quitYears'],
                'drinking_type' => $param['names'],
                'drinking_daily' => $param['values'],
            ]);
        }
        return $data;
    }

    public function Flavor($param)
    {
        $healthrecordDao = new LifeHealthDao();
        $data = $healthrecordDao->findByFlavorUid($param['memberUid']);
        if ($data) {
            $res = [
                'uid' => $param['memberUid'],
                'flavorKind' => $param['flavorKind'],
                'name' => $param['names'],
                'number' => $param['values'],
            ];
            $data = $healthrecordDao->updateFlavor($data, $res);
        } else {
            $data = $healthrecordDao->createFlavor([
                'uid' => $param['memberUid'],
                'flavorKind' => $param['flavorKind'],
                'name' => $param['names'],
                'number' => $param['values'],
            ]);
        }
        return $data;
    }
}
